﻿using Aspose.Cells;
using Common.Utility.AJ.Extension;
using LiveCharts;
using LiveCharts.Wpf;
using Microsoft.WindowsAPICodePack.Dialogs;
using NPOI.XSSF.UserModel;
using Stylet;
using SUT.PrintEngine.Utils;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using WeighingRecord.Models;
using WeighingRecord.Services;
using WeighingRecord.Views;

namespace WeighingRecord.ViewModels
{
    public class WeightTotalViewModel : Screen
    {
        private IWindowManager windowManager;

        public SeriesCollection SeriesCollection { get; set; }
        public string[] Labels { get; set; }
        public Func<double, string> Formatter { get; set; }

        public Brush WeekBorderBrush { get; set; }
        public Brush WeekBackground { get; set; }
        public Brush MonthBorderBrush { get; set; }
        public Brush MonthBackground { get; set; }
        public Brush YearBorderBrush { get; set; }
        public Brush YearBackground { get; set; }


        public string GblxXS
        {

            get
            {
                return $"{Common.Data.SQLDataAccess.GetGblxItems()[0]}过磅";
            }
        }

        public string GblxCG
        {

            get
            {
                return $"{Common.Data.SQLDataAccess.GetGblxItems()[1]}过磅";
            }
        }


        public WeightTotalViewModel(IWindowManager windowManager)
        {
            this.windowManager = windowManager;
            Getdata("1");
        }

        public void Getdata(string type)
        {
            BrushConverter conv = new BrushConverter();
            Brush FEF03E = conv.ConvertFromInvariantString("#FEF03E") as Brush;
            Brush F5F7F9 = conv.ConvertFromInvariantString("#F5F7F9") as Brush;
            Brush E1E6EC = conv.ConvertFromInvariantString("#E1E6EC") as Brush;

            WeekBorderBrush = E1E6EC;
            WeekBackground = F5F7F9;
            MonthBorderBrush = E1E6EC;
            MonthBackground = F5F7F9;
            YearBorderBrush = E1E6EC;
            YearBackground = F5F7F9;

            SeriesCollection = new SeriesCollection();
            //近年度前三名的所有数据
            var beginDateStr = DateTime.Now.AddMonths(-12).Date.ToString("yyyy-MM-dd HH:mm:ss");
            var endDateStr = DateTime.Now.Date.ToString("yyyy-MM-dd 23:59:59");
            var maxAbnormalData = Common.Utility.SettingsHelper.ZXWeighingRecordSettings.Settings["MaxAbnormalData"]?.Value.TryGetDecimal() ?? 100000m;
            var list = Common.Data.SQLDataAccess.GetLimits(beginDateStr, endDateStr, 3, maxAbnormalData).Where(p => p.IsFinish).ToList();

            if (list.Count <= 0) return;
            int count = 0;

            var endDate = DateTime.Parse(DateTime.Now.Date.ToString("yyyy-MM-dd 23:59:59"));
            if (type == "1")
            {
                WeekBorderBrush = FEF03E;
                WeekBackground = FEF03E;

                var beginDate = DateTime.Now.AddDays(-7).Date;
                list = list.Where(p => DateTime.Parse(p.Jzrq).Date >= beginDate && DateTime.Parse(p.Jzrq).Date <= endDate).ToList();
                var dateGroup = list.GroupBy(g => DateTime.Parse(g.Jzrq).Date).ToList();
                var carGroup = list.GroupBy(g => g.Ch).ToList();

                //创建前三名的数据接口
                foreach (var car in carGroup)
                {
                    SeriesCollection.Add(new ColumnSeries
                    {
                        Title = $"{car.Key} (近一周)",
                        Values = new ChartValues<double>()
                    });
                }

                //填充前三名的数据
                Labels = new string[dateGroup.Count];
                foreach (var date in dateGroup)
                {
                    Labels[count]=(date.Key.ToString("M-d"));

                    foreach (var car in carGroup)
                    {
                        var sumValue = date.Where(p => p.Ch == car.Key).Sum(s => double.Parse(s.Jz));
                        var info = SeriesCollection.FirstOrDefault(p => p.Title.Contains(car.Key));
                        //info.Values.Add(sumValue.ToString("0.##"));
                        info.Values.Add(double.Parse(sumValue.ToString("0.##")));
                    }
                    count++;
                }
            }
            else if (type == "2")
            {
                MonthBorderBrush = FEF03E;
                MonthBackground = FEF03E;

                var beginDate = DateTime.Now.AddDays(-30).Date;
                list = list.Where(p => DateTime.Parse(p.Jzrq).Date >= beginDate && DateTime.Parse(p.Jzrq).Date <= endDate).ToList();
                var dateGroup = list.GroupBy(g => DateTime.Parse(g.Jzrq).Date).ToList();
                var carGroup = list.GroupBy(g => g.Ch).ToList();

                //创建前三名的数据接口
                foreach (var car in carGroup)
                {
                    SeriesCollection.Add(new ColumnSeries
                    {
                        Title = $"{car.Key} (近一月)",
                        Values = new ChartValues<double>()
                    });
                }

                //填充前三名的数据
                Labels = new string[dateGroup.Count];
                foreach (var date in dateGroup)
                {
                    Labels[count] = (date.Key.ToString("M-d"));

                    foreach (var car in carGroup)
                    {
                        var sumValue = date.Where(p => p.Ch == car.Key).Sum(s => double.Parse(s.Jz));
                        var info = SeriesCollection.FirstOrDefault(p => p.Title.Contains(car.Key));
                        //info.Values.Add(sumValue.ToString("0.##"));
                        info.Values.Add(double.Parse(sumValue.ToString("0.##")));
                    }
                    count++;
                }
            }
            else if (type == "3")
            {
                YearBorderBrush = FEF03E;
                YearBackground = FEF03E;

                var dateGroup = list.GroupBy(g =>(new { DateTime.Parse(g.Jzrq).Year, DateTime.Parse(g.Jzrq).Month})).ToList();
                var carGroup = list.GroupBy(g => g.Ch).ToList();

                //创建前三名的数据接口
                foreach (var car in carGroup)
                {
                    SeriesCollection.Add(new ColumnSeries
                    {
                        Title = $"{car.Key} (近一年)",
                        Values = new ChartValues<double>()
                    });
                }

                //填充前三名的数据
                Labels = new string[dateGroup.Count];
                foreach (var date in dateGroup)
                {
                    Labels[count] = $"{(date.Key.Year.ToString())}-{(date.Key.Month.ToString())}";

                    foreach (var car in carGroup)
                    {
                        var sumValue = date.Where(p => p.Ch == car.Key).Sum(s => double.Parse(s.Jz));
                        var info = SeriesCollection.FirstOrDefault(p => p.Title.Contains(car.Key));
                        //info.Values.Add(sumValue.ToString("0.##"));
                        info.Values.Add(double.Parse(sumValue.ToString("0.##")));
                    }
                    count++;
                }

                //SeriesCollection = new SeriesCollection
                //{
                //    new ColumnSeries
                //    {
                //        Title = "日本红富士苹果(近一年)",
                //        Values = new ChartValues<double> { 10, 20, 30, 40,50,60,70,100 }
                //    },
                //    new ColumnSeries
                //    {
                //        Title = "东北大米(近一年)",
                //        Values = new ChartValues<double> { 100, 200, 300, 400, 500, 600, 700,1000 }
                //    },new ColumnSeries
                //    {
                //        Title = "山东包菜(近一年)",
                //        Values = new ChartValues<double> { 1000, 2000, 3000, 4000, 5000, 6000, 7000,10000 }
                //    }
                //};
                //Labels = new[] { "2020-01", "2020-02", "2020-03", "2020-04", "2020-05", "2020-06", "2020-07", "2020-08" };
            }
            //Formatter = value => value.ToString() + "T";
            Formatter = value => value.ToString("0.##");
        }

    }
}